package ngames.com.sushitv.action;

import android.app.Activity;
import android.app.Application;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import java.util.ArrayList;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.adapter.AnimeListAdapter;
import ngames.com.sushitv.adapter.SearchRequestAdapter;
import ngames.com.sushitv.adapter.VideoRankAdapter;
import ngames.com.sushitv.base.BaseListAction;
import ngames.com.sushitv.core.ModelCallback;
import ngames.com.sushitv.entity.Anime;
import ngames.com.sushitv.entity.ItemVideo;
import ngames.com.sushitv.entity.SearchPrompt;
import ngames.com.sushitv.entity.SearchRequestAll;
import ngames.com.sushitv.interfaces.OnItemClickListener;

/**
 * recycleView的重用
 */
public class SearchRequestAction extends BaseListAction {
    private String mDataID = "0";
    private RecyclerView mRecycler;
    private SwipeRefreshLayout mSwipeRefresh;
    private SearchRequestAdapter mAdapter;
    private int Page = 1;
    private String type;
    private String name;
    private String order;

    public SearchRequestAction(Activity activity, View baseView, int Page, String type, String name, String order) {
        super(activity, baseView);
        this.Page = Page;
        this.type = type;
        this.name = name;
        this.order = order;
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        mRecycler = (RecyclerView) mBaseView.findViewById(R.id.recycler_searchRequest);
        mSwipeRefresh = (SwipeRefreshLayout) mBaseView.findViewById(R.id.swipeRefresh_searchRequest);

        //每个item高度一致，可设置为true，提高性能
        //mRecycler.setHasFixedSize(true);
        //mRecycler.setLayoutManager(mLinearLayoutManager);
        mRecycler.setLayoutManager(new GridLayoutManager(mActivity, 1));
        if (type.equals("all")) {
            onRefreshData();
        } else {
            setListData();
        }
    }

    @Override
    public void onRefreshData() {
        videoRank();
    }

    private void videoRank() {
        getAppModel().AnimeList(order,type, name, Page + "", new ModelCallback<ArrayList<Anime>>() {
            @Override
            public void onBeforeRequest(String urlTag) {

            }

            @Override
            public void onSuccess(ArrayList<Anime> data) {
               /* mIsLoadingMore = false;
                //mSwipeRefreshLayout.setRefreshing(false);
                if ("0".equals(mDataID) && mAdapter != null) {
                    mAdapter.clearData();
                }
                if (isEmptyList(data)) {
                    return;
                }
                mDataID = data.get(data.size() - 1).getId();
                setListData(data);*/
                //Log.e("ok", "yes");
                setListData();
            }

            @Override
            public void onFailure(String errorEvent, String message) {
                Log.e("fail", errorEvent + "\n" + message);
            }
        });
    }


    public void setListData() {
        SearchRequestAll searchRequestAll = AppApplication.getSearchRequestAll();
        SearchRequestAll sr = new SearchRequestAll();
        //SearchRequestAll s=new SearchRequestAll();
        if (type.equals("all")) {
            mAdapter = new SearchRequestAdapter(mActivity, searchRequestAll);
            mRecycler.setAdapter(mAdapter);
        }
        if (type.equals("anime")) {
            try {
                sr.setAnimes(searchRequestAll.getAnimes());
                mAdapter = new SearchRequestAdapter(mActivity, sr);
            } catch (NullPointerException e) {

            }
            mRecycler.setAdapter(mAdapter);
        }
        if (type.equals("fan_made")) {
            try {
                sr.setTags(searchRequestAll.getTags());
                mAdapter = new SearchRequestAdapter(mActivity, sr);
            } catch (NullPointerException e) {

            }
            mRecycler.setAdapter(mAdapter);
        }


    }
}
